package org.nupter.mmruby;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.SaveCallback;

import java.io.*;


public class NewDogActivity extends Activity {
    private static final int SELECT_PHOTO = 100;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_new_dog);

        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.setType("image/*");
        startActivityForResult(photoPickerIntent, SELECT_PHOTO);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

        switch(requestCode) {
            case SELECT_PHOTO:
                if(resultCode == RESULT_OK){
                    Uri selectedImage = imageReturnedIntent.getData();
                    Log.d("TAG", selectedImage.toString());

                    try {
                        InputStream imageStream = getContentResolver().openInputStream(selectedImage);
                        Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
                        // The new size we want to scale to
                        final int REQUIRED_SIZE = 140;
                        BitmapFactory.Options o = new BitmapFactory.Options();
                        o.inJustDecodeBounds = true;

                        // Find the correct scale value. It should be the power of 2.
                        int width_tmp = o.outWidth, height_tmp = o.outHeight;
                        int scale = 1;
                        while (true) {
                            if (width_tmp / 2 < REQUIRED_SIZE
                                    || height_tmp / 2 < REQUIRED_SIZE) {
                                break;
                            }
                            width_tmp /= 2;
                            height_tmp /= 2;
                            scale *= 2;
                        }


                        // Decode with inSampleSize
                        BitmapFactory.Options o2 = new BitmapFactory.Options();
                        o2.inSampleSize = scale;
                        Bitmap bitmap =  BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);
                        ByteArrayOutputStream outputStream = null;

                        File file = new File(Environment.getExternalStorageDirectory() + "/tmp.jpg");
                        FileOutputStream fOut = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 30, fOut);
                        fOut.flush();
                        fOut.close();

                        Log.d("TAG", file.toString());


                        byte[] data = outputStream.toByteArray();
                        ParseFile parseFile = new ParseFile("dog.jpg", data);
                        parseFile.saveInBackground();
                        ParseObject object = new ParseObject("dog");
                        Log.d("TAG", "Save file");

                        object.put("image", parseFile);
                        object.saveInBackground(new SaveCallback() {
                            @Override
                            public void done(ParseException e) {

                                Toast.makeText(NewDogActivity.this, "Done", Toast.LENGTH_LONG).show();
                            }
                        });





                    } catch (Exception e){
                        e.printStackTrace();
                        Log.d("TAG", e.getMessage());
                    }
                }
        }
    }
}